package com.chen.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.annotation.OptLog;
import com.chen.common.Constants;
import com.chen.common.Constants;
import com.chen.common.OptTypeConst;
import com.chen.entity.Dict;
import com.chen.mapper.DictMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chen.common.Result;

import com.chen.service.MenuService;
import com.chen.entity.Menu;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 */
@RestController
@RequestMapping("/menu")
@Api(value="MenuController",tags = "菜单模块")
public class MenuController {

    @Resource
    private MenuService menuService;

    @Resource
    private DictMapper dictMapper;

    @Resource
    private RedisTemplate redisTemplate;

    // 新增或者更新
    @PostMapping
    @OptLog(optType = OptTypeConst.SAVE_OR_UPDATE)
    @ApiOperation(value = "新增或修改")
    public Result save(@RequestBody Menu menu) {
        menuService.saveOrUpdate(menu);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    @OptLog(optType = OptTypeConst.REMOVE)
    public Result delete(@PathVariable Integer id) {
        menuService.removeById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    @ApiOperation(value = "批量删除")
    @OptLog(optType = OptTypeConst.REMOVE_BATH)
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        menuService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping("/ids")
    public Result findAllIds() {
        return Result.success(menuService.list().stream().map(Menu::getId));
    }

    @GetMapping
    @ApiOperation(value = "查询所有")
    public Result findAll(@RequestParam(defaultValue = "") String name) {
        return Result.success(menuService.findMenus(name));
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(menuService.getById(id));
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public Result findPage(@RequestParam String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);
        queryWrapper.orderByDesc("id");
        return Result.success(menuService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }


    /**
     * redis中查询
     * @return
     */
    @GetMapping("/icons")
    @ApiOperation(value = "查询icon")
    public Result getIcons() {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", Constants.DICT_TYPE_ICON);
        List<Dict> iconDict = (List<Dict>) redisTemplate.opsForValue().get(Constants.DICT_TYPE_ICON);
        if (iconDict == null){
            List<Dict> dictList = dictMapper.selectList(null);
            redisTemplate.opsForValue().set(Constants.DICT_TYPE_ICON,dictList);
            iconDict=dictList;
            System.out.println("查询走的是mysql=========>");
            return Result.success(iconDict);
        }else{
            System.out.println("查询走的是redis==============>");
            return Result.success(iconDict);
        }


    }





}

